package com.coco.service.impl;

import com.coco.dao.CommentsMapper;
import com.coco.dao.UserMapper;
import com.coco.model.CommentsInfo;
import com.coco.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author GHOSTLaycoo
 * @date 2020/5/27 - 10:21
 */
@Service
public class CommentsServiceImpl implements CommentsService{

    @Autowired
    private CommentsMapper commentsMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public int save(CommentsInfo commentsInfo) {
        commentsInfo.setId(UUID.randomUUID().toString());
        commentsInfo.setCreateTime(new Date());
        int save = commentsMapper.save(commentsInfo);
        if(save>0){
            return save;
        }
        return 0;
    }

    @Override
    public List<CommentsInfo> selectComments(String id) {
        List<CommentsInfo> commentsInfos = commentsMapper.selectComments(id);
        commentsInfos.forEach(e->{
            e.setUserName(userMapper.getUserById(e.getUserId()).getName());
        });
        return commentsInfos;
    }
}
